package com.wego.controller.manager;

import com.wego.controller.manager.base.BaseController;
import com.wego.service.base.BaseService;
import com.wego.common.bean.PageBean;
import com.wego.common.bean.ResultBean;
import com.wego.common.enumeration.ResultEnum;
import com.wego.common.minio.MinioUtil;
import com.wego.common.utils.QueryUtil;
import com.wego.common.utils.ResultBeanUtil;
import com.wego.converter.UserConverter;
import com.wego.entity.domain.User;
import com.wego.entity.query.UserQuery;
import com.wego.entity.vo.manager.UserVO;
import com.wego.service.UserService;
import java.time.LocalDateTime;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

/**
 * 用户控制器
 * @author: hc
 * @date: 2023/7/2
 */
@Slf4j
@Controller
@RequestMapping("/manager/user")
public class UserController extends BaseController<User> {
    @Autowired
    private UserService userService;

    @Autowired
    private UserConverter userConverter;

    @Autowired
    private MinioUtil minioUtil;

    public UserController(BaseService<User> service) {
        super(service);
    }

    /**
     * 省份列表
     * @param userQuery
     * @param model
     * @param resultBean
     * @return
     */
    @GetMapping("/list")
    public String list(UserQuery userQuery, Model model, @ModelAttribute("resultBean") ResultBean resultBean) {
        //构造动态查询条件
        String keywords = userQuery.getKeywords();
        if (keywords != null) {
            if (userQuery.getNickname() == null) {
                userQuery.setNickname(keywords);
            }
            if (userQuery.getPhone() == null) {
                userQuery.setPhone(keywords);
            }
        }

        PageBean<User> userPageBean = userService.selectPage(userQuery);
        final PageBean<UserVO> pageBean = userConverter.userPageBean2UserVOPageBean(userPageBean);

        //分页时携带上查询条件
        String params = QueryUtil.genQueryCondition(userQuery);
        pageBean.setUrl("manager/user/list?" + params);
        model.addAttribute("pageBean", pageBean);

        //查询条件数据回显时使用
        model.addAttribute("userQuery", userQuery);

        //增删改操作完返回到列表页面的提示信息
        if (resultBean != null) {
            model.addAttribute("resultBean", resultBean);
        }

        return "manager/user";
    }

    /**
     * 具体执行添加修改操作
     * @param user
     * @return
     */
    @PostMapping("/doAddUpdate")
    public String doAddUpdate(User user, MultipartFile avatarFile, RedirectAttributes redirectAttributes) {
        if (avatarFile != null && avatarFile.getSize() > 0) {
            final String FileName = minioUtil.putObject2(avatarFile, "user", null);
            user.setAvatar(FileName);
        }

        user.setUpdateTime(LocalDateTime.now());

        ResultBean resultBean;
        Long id = user.getId();

        if (id != null) {
            //修改
            boolean res = userService.updateByPrimaryKeySelective(user);
            resultBean = ResultBeanUtil.build(res, ResultEnum.UPDATE_SUCCESS, ResultEnum.UPDATE_EXCEPTION);
        } else {
            //添加
            user.setCreateTime(LocalDateTime.now());
            boolean res = userService.insertSelective(user);
            resultBean = ResultBeanUtil.build(res, ResultEnum.INSERT_SUCCESS, ResultEnum.INSERT_EXCEPTION);
        }

        //返回列表页面时，对话框中显示的提示信息
        redirectAttributes.addFlashAttribute("resultBean", resultBean);

        //重定向
        return "redirect:list";
    }

}
